package com.chazz.servlet;

import com.chazz.entity.User;
import com.chazz.service.UserService;
import com.chazz.service.impl.UserServiceImpl;
import com.chazz.utils.EmailValidation;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/FindpasswordServlet.do")
public class FindpasswordServlet extends HttpServlet {
    private static UserService userService=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email=req.getParameter("email");
        User user=null;
        PrintWriter out=resp.getWriter();
        try {

            user=userService.findUserByEmail(email);
            if (user==null){
                out.write("false");
            }else{
                out.write("true");
                String str = EmailValidation.randomStr();//随机产生的验证码
                EmailValidation.load(email,str); //发送邮件
                System.out.println(str);
                HttpSession session=req.getSession();
                session.setAttribute("passwordstringBuilder",str);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
